package com.melody.controller;

import com.melody.entry.MusicInfo;
import com.melody.entry.PlaylistMusic;
import com.melody.entry.Result;
import com.melody.entry.UserFavorite;
import com.melody.service.PlaylistInfoService;
import com.melody.service.UserFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userfavorite")
public class UserfavoriteController {
    @Autowired
    private UserFavoriteService userFavoriteService;
    @Autowired
    private PlaylistInfoService playlistInfoService;

    /**
     *  批量删除用户喜欢的音乐
     *  @param musicIds
     *  @param userId
     * */
    @DeleteMapping("/deleteMany")
    public Result batchDeleteUserFavorite(@RequestParam List<Long> musicIds, Integer userId ) {
        List<UserFavorite> userFavorites = userFavoriteService.batchDeleteUserFavorite(musicIds, userId);
        if (userFavorites != null) {
            return Result.success(userFavorites); // 返回批量删除成功的音乐列表
        } else {
            return Result.error("服务器繁忙，删除失败"); // 返回错误信息
        }
    }

    /**
     * 获取用户喜欢的音乐
     * @param userId
     * @return 喜欢的音乐列表
     * */
    @GetMapping("/getUserFavorite")
    public Result<List<MusicInfo>> getUserFavoriteByUserId(@RequestParam Integer userId) {
        List<MusicInfo> musicInfos = playlistInfoService.getFavoriteByUserId(userId);
        if (musicInfos != null && !musicInfos.isEmpty()) {
            return Result.success(musicInfos);
        } else {
            return null;
        }
    }

    /**
     * 添加用户喜欢的音乐
     * @param musicId
     * @param userId
     * @return 添加结果
     * */
    @PostMapping("/addUserFavorite")
    public Result<String> addUserFavorite(@RequestParam Integer musicId, @RequestParam Integer userId) {

        String result = userFavoriteService.addUserFavorite(musicId, userId);
        return result != null ? Result.success(result) : Result.error("添加失败");
    }
}
